from sklearn.svm import SVC
import numpy as np
from sklearn import datasets
import matplotlib.pyplot as plt
iris = datasets.load_iris()
print(iris)

X = iris['data'][:,(2,3)]
y = iris['target']

setosa_or_versicolor = (y==0)|(y==1)
X = X[setosa_or_versicolor]
y = y[setosa_or_versicolor]
print(X)
print(y)
svm_clf = SVC(kernel='linear')
svm_clf.fit(X,y)

def print_boundary(xmin,xmax):
    w = svm_clf.coef_[0]
    b = svm_clf.intercept_
    print(w)
    print(b)

    x0 = np.linspace(xmin,xmax,200)
    decision_boundary = -w[0]/w[1]*x0-b/w[1]
    margin = 1/w[1]
    up = decision_boundary + margin
    down = decision_boundary - margin
    svs = svm_clf.support_vectors_
    plt.scatter(svs[:,0],svs[:,1],s=180)
    plt.plot(x0,decision_boundary,'k-',linewidth=2)
    plt.plot(x0,up,'k--',linewidth=2)
    plt.plot(x0,down,'k--',linewidth=2)

    plt.plot(X[:,0],X[:,1],'bs')
    plt.show()

print_boundary(0,5.5)